<template>
  <div class="goodsadd">
    <div class="head">
      <h3>添加商品</h3>
    </div>
    <div class="main">
      <div class="item">
        <div style="margin-right: 10px">商品名称</div>
        <div style="width: 240px"><el-input v-model="name"></el-input></div>
      </div>
      <div class="item">
        <div style="margin-right: 10px">商品分类</div>
        <div>
          <el-select v-model="type" placeholder="请选择">
            <el-option
              v-for="item in categories"
              :key="item.cateName"
              :value="item.cateName"
            >
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="item">
        <div style="margin-right: 10px">商品价格</div>
        <div>
          <el-input-number
            v-model="price"
            @change="handleChange"
            :min="0.01"
            label="描述文字"
          ></el-input-number>
        </div>
      </div>
      <div class="item">
        <div style="margin-right: 10px">商品照片</div>
        <div>
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1:5000/goods/goods_img_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
      </div>

      <div class="item">
        <div style="margin-right: 10px">商品描述</div>
        <div style="width: 240px">
          <el-input
            type="textarea"
            v-model="desc"
            :rows="2"
            placeholder="请输入内容"
          >
          </el-input>
        </div>
      </div>
      <el-button type="primary" @click="clickadd">添加商品</el-button>
    </div>
  </div>
</template>

<script>
import { $_goodscategories, $_addgoods } from "@/apis/goods";

export default {
  created() {
    $_goodscategories().then((res) => {
      this.categories = res.data.categories;
    });
  },
  data() {
    return {
      name: "", //商品名称
      type: "", //商品分类
      desc: "", //商品描述
      newimage: "", //上传成功后新的图片名
      price: "0", //价格
      imageUrl: "", //回显的图片src
      categories: [], //分类名称数组
    };
  },
  methods: {
    handleChange(value) {
      console.log(value);
    },
    handleAvatarSuccess(res, file) {
      console.log(res, file);
      this.newimage = res.imgUrl;
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      let types = ["image/jpeg", "image/png", "image/webp"];
      const isJPG = types.includes(file.type);
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG、png、webp 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    clickadd() {
      console.log(this.name, this.type, this.price, this.newimage, this.desc);
      $_addgoods({
        name: this.name,
        category: this.type,
        price: this.price,
        imgUrl: this.newimage,
        goodsDesc: this.desc,
      }).then((res) => {
        console.log(res);
        if (res.data.code == 0) {
          this.$message(res.data.msg);
        }
      });
    },
  },
};
</script>

<style lang='less' scoped>
.goodsadd {
  width: 98%;
  height: 800px;
  padding: 10px;
  background-color: #fff;
  box-shadow: 0 0 20px #212121;
  .head {
    height: 50px;
    padding: 0 10px;
    margin-bottom: 10px;
    border-bottom: 1px solid #bdbdbd;
  }
  .main {
    padding: 20px;
    .item {
      margin: 20px;
      display: flex;

      .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
      }
      .avatar-uploader .el-upload:hover {
        border-color: #409eff;
      }
      .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
      }
      .avatar {
        width: 178px;
        height: 178px;
        display: block;
      }
    }
  }
}
</style>